var goal={};
var count = 5;
ps = document.getElementsByTagName("p");
guess = document.getElementsByClassName("number");
function ranNumber(){
    var temp;
    for(var i=0;i<4;i++){
        if(i==0){ goal[i] = Math.floor(Math.random()*10); }
        else{
            while(true){
                temp = Math.floor(Math.random()*10);
                if(isOK(temp)){
                    goal[i] = temp;
                    break;
                }
            }

        }
        console.log(goal[i]);
    }
}
function isOK(number){
    for(var i=0;i<4;i++){
        if(goal[i]==number){
            return false;
        }
    }
    return true;
}
function NotNull(){
    for(var i=0;i<4;i++){
        if(guess[i].value == ""||guess[i].value == " "){
            return false;
        }
    }
    return true;
}
function GuessNow(){
    if(count>0&&NotNull()){
        document.getElementById("count").innerHTML = "count: "+(--count);
        var a = 0,b = 0;
        for(var i=0;i<4;i++){
            for(var j=0;j<4;j++){
                if(guess[i].value==goal[j]){
                    if(i==j){ a++; }
                    else{ b++; }
                }
            }
        }
        ps[4-count].innerHTML = (5-count)+":"+guess[0].value+guess[1].value+guess[2].value+guess[3].value+" "+a+"A"+b+"B";
        if(a==4||count==0){
            if(a==4){ window.alert("win!"); }
            else{ window.alert("lose!"); }
            document.getElementById("start").style.visibility = "visible";
        }
    }
}
function Start(){
    ranNumber();
    count = 5;
    document.getElementById("count").innerHTML = "count: "+ count;
    for(var i=0;i<4;i++){
        guess[i].value = "";
    }
    for(var j=0;j<5;j++){
        ps[j].innerHTML = "";
    }
    document.getElementById("start").style.visibility = "hidden";
}   